<template>
	<view class="follow">
		<g-navbar title="关注小律优创" :clickBack="onClickBack" fontColor="#ffffff" backgroundColor="#1d79f6"></g-navbar>
		<view class="qrcode-wrapper">
			<image src="http://lz-app-1311894087.cos.ap-nanjing.myqcloud.com/20230414162539-qrcode.jpg"
				class="qrcode" />
		</view>
		<view class="btn-wrapper">
			<g-button :width="686" :height="100" btn_text="我已关注，返回首页" :borderRadius="14"
				@click="global.authorization('/pages/follow/follow')" />
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { onReady, onLoad } from '@dcloudio/uni-app';
	import { Ref, ref } from 'vue';
	import REST_URL from '@/http/http.restUrl';
	import { useStore } from 'vuex';
	import global from '@/global';
	import { VERSION_TYPE } from '@/enum/version';
	import { ROUTE_TYPE } from '@/enum/router';
	import { GET_STORE_USERINFO } from '@/store/gettersType';
	const wx_code : Ref<string> = ref('');


	onLoad((options) => {
		console.log('onLoad======>', options);
		const searchParams = new URL(window.location.href).searchParams;
		const params = Object.fromEntries(searchParams);
		console.log(params)
		wx_code.value = params.code;
		if (wx_code.value) {
			confirmFollow();
		}
		global.resetWebUrl('/pages/follow/follow');
	})

	onReady(() => {
		if (!wx_code.value) {
			uni.showModal({
				title: '未关注公众号',
				content: '当前账号未关注公众号,为保证交易信息正常送达，请长按识别二维码关注小律优创公众号',
				confirmText: '确认',
				showCancel: false,
			})
		}
	})





	const store = useStore();

	const onClickBack = () => {
		console.log('onClickBack')
		global.authorization('/pages/follow/follow')
	}

	const jumpHome = () => {
		if (store.state.version == VERSION_TYPE.EXP_VERSION) {
			uni.router.route('/pages/home/home');
		} else {
			uni.router.route('/pages/statistics/statistics');
		}
	}


	const confirmFollow = async () => {
		try {
			await uni.http.POST(REST_URL.checkFollow, {
				customer_id: store.getters[GET_STORE_USERINFO].id,
				code: wx_code.value
			})
			jumpHome();

		} catch (e) {
			if (e.status === -900) {
				uni.showModal({
					title: '当前微信已绑定其他账号',
					content: e.msg,
					confirmText: '进入首页',
					cancelText: '更换微信绑定',
					success: (res) => {
						if (res.confirm) {
							jumpHome();
						} else {
							uni.router.route({
								url: '/pages/bind_wechat_authorization/bindWechatAuthorization',
								type:ROUTE_TYPE.REDIRECTTO,
							})
						}
					}
				})
			}
		}




		// uni.router.route('/pages/home/home');
		// window.location.replace(`http://customer.itkn.net.cn/#/pages/home/home`);
	}
</script>

<style scoped lang="scss">
	@import "./follow.scss";
</style>